package com.otomotion.research.ants.Behavior;

import com.otomotion.research.ants.request.Request;
import com.otomotion.research.ants.information.Surroundings;
import com.otomotion.research.ants.entities.AntBrain;

import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Mark the intern
 * Date: 8-Aug-2007
 * Time: 4:59:59 PM
 */
public class Behavior {

    private AntBrain owner;
    private Action trueAction;
    private Action falseAction;
    private Vector<Event> events;
    private Operator operator;

    public Behavior(AntBrain owner) {
        this.owner = owner;
    }

    private boolean evaluateCausality(Surroundings surroundings) {
        boolean behaviorHappens;
        if (operator.equals(Operator.AND)) {
            behaviorHappens = true;
            for (Event event : events) {
                if (!event.occured(surroundings)) {
                    behaviorHappens = false;
                    break;
                }
            }
        } else { //Operator is OR
            behaviorHappens = false;
            for (Event event : events) {
                if (event.occured(surroundings)) {
                    behaviorHappens = true;
                    break;
                }
            }
        }
        return behaviorHappens;
    }

    public Request run(Surroundings surroundings) {
        if(evaluateCausality(surroundings)) {
            return trueAction.perform(surroundings);
        } else {
            return falseAction.perform(surroundings);
        }
    }
}
